package com.swsc.xapp.xroom;

import com.swsc.xapp.offline.lhttpd.ApiException;

import java.util.List;

public class CorporationService extends BaseService{
    private static volatile CorporationService instance;

    public static CorporationService getInstance(){
        if (instance == null) {
            synchronized (CorporationService.class) {
                if (instance == null) {
                    instance = new CorporationService();
                }
            }
        }
        return instance;
    }
    private CorporationService(){}
    public void deleteByTenantOrgan(Integer tenantNo, Integer organNo) {
        db.corporationDao().deleteByTenantOrgan(tenantNo, organNo);
    }

    public void saveAll(CorporationDO ... corporationDos) {
        db.corporationDao().saveAll(corporationDos);
    }

    public void deleteAll(){
        db.corporationDao().deleteAll();
    }

    public List<CorporationDO> findByCondition(Integer tenantNo, Integer organNo, List<Integer> organNoIn){
        StringBuilder sqlBuilder = parseBaseParamsBuilder(tenantNo, organNo);
        if(null != organNoIn && organNoIn.size() > 0){
            sqlBuilder.append(" AND organ_no in (");
            for(int i = 0 ; i < organNoIn.size(); i++){
                if(i > 0){
                    sqlBuilder.append(", ");
                }
                sqlBuilder.append(organNoIn.get(i));
            }
            sqlBuilder.append(")");
        }
        return db.corporationDao().doRawQuery(sqlBuilder.toString());
    }

    public CorporationDO checkStationByOrganNo(Integer organNo) {
        List<CorporationDO> corporationDos = findByCondition(null, organNo, null);
        ApiException.isFalse(null == corporationDos || corporationDos.size() < 1, "组织不存在");
        CorporationDO corporationDo = corporationDos.get(0);
        ApiException.isFalse(null == corporationDo, "电站不存在");
        return corporationDo;
    }
}
